package com.example.questionexam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.mapper.ExamMapper;
import com.example.questionexam.model.dto.exam.ExamQueryRequest;
import com.example.questionexam.model.entity.Exam;
import com.example.questionexam.service.ExamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author cola
 * @description 针对表【question(题目)】的数据库操作Service实现
 * @createDate 2024-05-27 19:27:21
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam>
        implements ExamService{

    @Resource
//    private AnswerMapper answerMapper;
    private ExamMapper examMapper;

    @Override
    public QueryWrapper<Exam> getQueryWrapper(ExamQueryRequest examQueryRequest) {
        if (examQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = examQueryRequest.getId();
        String teacher_name = examQueryRequest.getTeacher_name();

        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null && id != 0, "id", id);
        queryWrapper.eq(StringUtils.isNotBlank(teacher_name), "teacher_name", teacher_name);
        return queryWrapper;
    }

    @Override
    public void validExam(Exam exam, boolean isAdd) {
        ThrowUtils.throwIf(exam == null, ErrorCode.PARAMS_ERROR);
        Long id = exam.getId();
        String teacher_name = exam.getTeacher_name();
        String teacher_class = exam.getTeacher_class();
        String exam_quanxian = exam.getExam_quanxian();

        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(teacher_name,teacher_class), ErrorCode.PARAMS_ERROR);
        } else {
            ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isNotBlank(exam_quanxian) && exam_quanxian.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "输入错误");
        }
    }


}




